# -*- coding: utf-8 -*-

# scrapy crawl gs - a username = 2015218001 - a password = youdontknow

import scrapy, sys, os, re
from tjugs.items import TjugsItem

class User(object):
    pass

class GsSpider(scrapy.Spider):
    name = "gs"
    allowed_domains = ["202.113.5.137"]
    start_urls = (
        'http://202.113.5.137/stuslls/achi/achi',
    )
    main_url = 'http://202.113.5.137/stuslls/main/main'
    user = User()
    setattr(user, 'username', '2015218001')
    setattr(user, 'password', 'test')

    def __init__(self, username='', password=''):
        self.user.username = username or self.user.username
        self.user.password = password or self.user.password


    def start_requests(self):
        return [scrapy.FormRequest("http://202.113.5.137/stuslls/login/login!login", formdata={'loginBean.username': self.user.username, 'loginBean.password': self.user.password}, callback=self.logged_in)]

    def logged_in(self, response):
        # pprint(vars(response))
        # # open('login_success.txt', 'a+').write(vars(response).__str__())
        # open('login_error.txt', 'a+').write(vars(response).__str__())
        # print 111111111

        if not response._url.startswith(self.main_url):
            print("用户名或密码错误")
            os._exit(0)

        # 登陆成功继续开始爬数据
        for url in self.start_urls:
            yield self.make_requests_from_url(url)

    def parse(self, response):
        rootTable = response.xpath('//table[contains(@class, "left_txt2")]/tbody/tr')
        items = []

        del rootTable[0]

        for item in rootTable:
            clazz = TjugsItem()
            clazz['id'] = re.sub('[\r\n\|\b\t]', '',  item.xpath('td[2]/text()').extract()[0])
            clazz['name'] = re.sub('[\r\n\|\b\t]', '',  item.xpath('td[3]/text()').extract()[0])
            clazz['credit'] = re.sub('[\r\n\|\b\t]', '',  item.xpath('td[4]/text()').extract()[0])
            # clazz['time'] = re.sub('[\r\n\|\b\t]', '',  item.xpath('td[5]/text()').extract()[0])
            clazz['grade'] = re.sub('[\r\n\|\b\t]', '',  item.xpath('td[8]/text()').extract()[0])
            items.append(clazz)
        # item['title'] = response.xpath('//title/text()[1]').extract()
        # form = response.xpath('//form[@id="login"][1]')
        # item['action'] = form.xpath('@action').extract()
        # print title, action
        # print items
        return items
